﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using DemoApp.Model;
using DynamicViewModel;

namespace DemoApp.View
{
    /// <summary>
    /// Interaction logic for ContactView.xaml
    /// </summary>
    internal partial class ContactView : UserControl
    {
        public static readonly RoutedCommand ClearNamesCommand  = new RoutedCommand();
        public static readonly RoutedCommand NavigateUriCommand = new RoutedCommand();

        public ContactView()
        {
            InitializeComponent();

            // Create a new instance. Once created
            // do not call methods directly on this
            // object. (Use the dynamic viewModel).
            var instance  = new ContactDetails() {
                FirstName = "Nikos",
                LastName  = "Baxevanis"
            };

            dynamic viewModel = new DynamicViewModel<ContactDetails>(instance);

            // Wire the ClearNamesCommand from the view to the viewModel.
            CommandManager.RegisterClassCommandBinding(typeof(ContactView),
                new CommandBinding(
                    ClearNamesCommand,
                    (sender, e) => { viewModel.ClearFullName(); },
                    (sender, e) => { e.CanExecute = !String.IsNullOrWhiteSpace(viewModel.FullName); }));

            // Wire the NavigateUriCommand from the view to the viewModel.
            CommandManager.RegisterClassCommandBinding(typeof(ContactView),
                new CommandBinding(
                    NavigateUriCommand,
                    (sender, e) => { viewModel.NavigateTo(e.Parameter); },
                    (sender, e) => { e.CanExecute = String.IsNullOrWhiteSpace(viewModel.FullName); }));

            DataContext = viewModel;
        }
    }
}